package cn.edu.imnu.controller.web;

import cn.edu.imnu.model.WordUser;
import cn.edu.imnu.service.WordUserService;
import common.model.DataBootstrapTable;
import common.model.ResponseModelBootstrapTable;
import common.util.ProxyUtil;
import common.util.SysConstants;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 *
 * @author ranhongmin--2018/3/10
 * Description:
 */
@Controller
@RequestMapping("wordUserAction")
public class WordUserController {
	@Resource
	private WordUserService userService;


	/**
	 * main
	 *
	 * @param request
	 * @param dataBootstrapTable
	 * @return
	 */
	@RequestMapping("main.json")
	@ResponseBody
	public ResponseModelBootstrapTable main(HttpServletRequest request, DataBootstrapTable dataBootstrapTable) {
		return userService.main(dataBootstrapTable);
	}

	/**
	 * add
	 *
	 * @param request
	 * @param info
	 * @return
	 */
	@RequestMapping("add.json")
	@ResponseBody
	public ResponseModelBootstrapTable add(HttpServletRequest request, WordUser info) {
		return userService.doAction(info, "1");
	}


	/**
	 * update
	 *
	 * @param request
	 * @param info
	 * @return
	 */
	@RequestMapping("update.json")
	@ResponseBody
	public ResponseModelBootstrapTable update(HttpServletRequest request, WordUser info) {
		return userService.doAction(info, "2");
	}

	/**
	 * del
	 *
	 * @param request
	 * @param info
	 * @return
	 */
	@RequestMapping("del.json")
	@ResponseBody
	public ResponseModelBootstrapTable del(HttpServletRequest request, WordUser info) {
		return userService.doAction(info, "3");
	}


	/**
	 * login
	 *
	 * @param request
	 * @param wordUser
	 * @return
	 */
	@RequestMapping("login.json")
	@ResponseBody
	public ResponseModelBootstrapTable login(HttpServletRequest request, WordUser wordUser) throws Exception {
		ResponseModelBootstrapTable model = new ResponseModelBootstrapTable();
		String username = wordUser.getUsername();
		String password = wordUser.getPassword();


		try {
			SecurityUtils.getSubject().login(new UsernamePasswordToken(username, password));
			WordUser user = (WordUser) ProxyUtil.getAgentTarget(SecurityUtils.getSubject().getSession().getAttribute(SysConstants.SESSION_USER_KEY));
			request.getSession().setAttribute("user_id", user.getId());
			request.getSession().getAttribute("user_id");
			model = userService.doLogin(username, password);
			model.success("登录成功!");

		} catch (Exception e) {
			//
			model.error("用户名或密码错误！");
			// } finally {
			// 	try {
			// 	} catch (Exception e) {
			// 		logger.debug("Controller:记录日用户登录日志发生异常，系统默认忽略。");
			// 	}
		}
		return model;
	}

	/**
	 * 用户登陆检查使用
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("isLogin.json")
	@ResponseBody
	public ResponseModelBootstrapTable findMenus(HttpServletRequest request) {

		WordUser user = (WordUser) request.getSession().getAttribute(
				SysConstants.SESSION_USER_KEY);
		ResponseModelBootstrapTable model = userService.checkLogin(user);
		return model;

	}

	/**
	 * 退出清除session
	 *
	 * @param request
	 */
	@RequestMapping("logout.json")
	@ResponseBody
	public void logout(HttpServletRequest request) {
		request.getSession().invalidate();
	}


}
